import { useRequest } from "ahooks";
import { useModel } from '@umijs/max';
import { getFetch } from "./doFetch";

export default function useMyProjectList() {
    const {
        initialState,
        setInitialState,
    } = useModel('@@initialState');

    const { 
        myProList,
        fetchProject,
     } = initialState;
     const getMyPro = useRequest(
       async (params) => {
          const res = await getFetch({url:"/project/pmList",params:{}});
          const ids = res?.data?.dataList.map(it=>it.id);
          const allProjects = await fetchProject(params);
          return allProjects?.filter(project=>ids.includes(project.id));
        },
        {
          manual: true,
          onSuccess: (res,params) => {
            setInitialState((s) => ({
              ...s,
              myProList: res,
            }));
          },
        },
      );

    return { initialState, setInitialState, myProList, getMyPro };
}